package com.example.controller;

import com.example.common.Result;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.service.AdminService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
public class WebController {

    @Resource
    AdminService adminService;
    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account loginAccount=null;
        if(RoleEnum.ADMIN.name().equals(account.getRole())){
            loginAccount=adminService.login(account);
        }else if (RoleEnum.USER.name().equals(account.getRole())){
            loginAccount=userService.login(account);
        }
        return Result.success(loginAccount);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
       if(RoleEnum.ADMIN.name().equals(account.getRole())){
           adminService.updatePassword(account);
       }else if(RoleEnum.USER.name().equals(account.getRole())){
           userService.updatePassword(account);
       }
       return Result.success();
    }
}
